package com.open.capacity.common.controller;

import com.open.capacity.commons.Globals;
import com.open.capacity.commons.Result;
import com.open.capacity.license.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 用于生成证书文件，不能放在给客户部署的代码里
 *
 * @author zifangsky
 * @date 2018/4/26
 * @since 1.0.0
 */
@RestController
@RequestMapping("/license")
public class LicenseCreatorController {

    /**
     * 证书生成路径
     */
    @Value("${license.licensePath}")
    private String licensePath;

    /**
     * 查询是否授权
     */
    @RequestMapping(value = "/getAuthorizedInfo")
    public Result getAuthorizedInfo() {
        return Result.succeed(Globals.authorized);
    }

    /**
     * 获取服务器硬件信息
     *
     * @param osName 操作系统类型，如果为空则自动判断
     * @return com.ccx.models.license.LicenseCheckModel
     * @author zifangsky
     * @date 2018/4/26 13:13
     * @since 1.0.0
     */
    @RequestMapping(value = "/getServerInfos", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public Result<LicenseCheckModel> getServerInfos(@RequestParam(value = "osName", required = false) String osName) {
        //操作系统类型
        if (StringUtils.isBlank(osName)) {
            osName = System.getProperty("os.name");
        }
        osName = osName.toLowerCase();

        AbstractServerInfos abstractServerInfos = null;

        //根据不同操作系统类型选择不同的数据获取方法
        if (osName.startsWith("windows")) {
            abstractServerInfos = new WindowsServerInfos();
        } else if (osName.startsWith("linux")) {
            abstractServerInfos = new LinuxServerInfos();
        } else {//其他服务器类型
            abstractServerInfos = new LinuxServerInfos();
        }
        LicenseCheckModel data = abstractServerInfos.getServerInfos();
        return Result.succeed(data);
    }

    /**
     * 生成授权码
     *
     * @since 1.0.0
     */
    @RequestMapping(value = "/create")
    public Result create(String machineCode) {
        LicenseCreator licenseCreator = new LicenseCreator(machineCode);
        String result = licenseCreator.getLicense();

        if (StringUtils.isNotEmpty(result)) {
            return Result.succeed(result, "生成成功");
        } else {
            return Result.failed("证书文件生成失败");
        }
    }

    /**
     * 激活
     *
     * @since 1.0.0
     */
    @RequestMapping(value = "/activate")
    public Result activate(String machineCode, String licenseCode) {
        LicenseCreator licenseCreator = new LicenseCreator(machineCode);
        boolean result = licenseCreator.generateLicense(licensePath, licenseCode);

        if (result) {
            return Result.succeed("生成授权文件成功");
        } else {
            return Result.failed("授权文件生成失败");
        }
    }

}
